#include "ccommunication.h"
#include "cadlight.h"

CCommunication::CCommunication(QObject *parent) : QObject(parent)
{
}

void CCommunication::clearDevice()
{
    _light_ad.clearDevice();
    _curtain_ad.clearDevice();
    _air_ad.clearDevice();
    _door_ad.clearDevice();
    _scene_ad.clearDevice();
    _tv_ad.clearDevice();
    _music_ad.clearDevice();
}

void CCommunication::addDevice(QString id,QString device_name,DEVICE_AUDIOCONTROL device_type,QString ops)
{
    DEVICE_OPERATION *d = new DEVICE_OPERATION();
    d->_id = id;
    d->_name = device_name;
    d->_device_type = device_type;
    d->_support_operation = ops.split(";");

    if(device_type == LIGHT){
        _light_ad.addDevice(d);
    }
    else if(device_type == CURTAIN){
        _curtain_ad.addDevice(d);
    }
    else if(device_type == AIR){
        _air_ad.addDevice(d);
    }
    else if(device_type == SCENE){
        _scene_ad.addDevice(d);
    }
    else if(device_type == DOOR){
        _door_ad.addDevice(d);
    }
    else if(device_type == TV_REMOTE){
        _tv_ad.addDevice(d);
    }
    else if(device_type == MUSIC_REMOTE){
        _music_ad.addDevice(d);
    }
    else
    {
        delete d;
    }
}

QStringList CCommunication::splitShortWord(QString words)
{
    return words.split(",");
}

QString CCommunication::search_control_zone(QString word)
{
    foreach (QString l, DEVICE_OPERATION::LIST_AREA_NAME) {
        if(word.contains(l)){
            return l;
        }
    }

    return "";
}

DEVICE_OPERATION * CCommunication::wordAnalyse(QString word)
{
    // 1. 判断属于哪个设备
    // 2. 设备判断属于何种操作
    // 3. 是否属于场景描述

    word = word.remove("。");

    DEVICE_OPERATION *audio_do = nullptr;    

    if(nullptr!= (audio_do = _call_ad.parseWord(word)))
    {

    }
    else if(nullptr!= (audio_do = _scene_ad.parseWord(word)))
    {
        _scene_ad.actionAudionControl(audio_do);
    }
    else if(nullptr!= (audio_do = _light_ad.parseWord(word)))
    {
        _light_ad.actionAudionControl(audio_do);
    }
    else if(nullptr!= (audio_do = _curtain_ad.parseWord(word)))
    {
        _curtain_ad.actionAudionControl(audio_do);
    }
    else if(nullptr!= (audio_do = _air_ad.parseWord(word)))
    {
        _air_ad.actionAudionControl(audio_do);
    }
    else if(nullptr!= (audio_do = _door_ad.parseWord(word)))
    {
        _door_ad.actionAudionControl(audio_do);
    }    
    else if(nullptr!= (audio_do = _music_ad.parseWord(word)))
    {
        _music_ad.actionAudionControl(audio_do);
    }
    else if(nullptr!= (audio_do = _tv_ad.parseWord(word)))
    {
        _tv_ad.actionAudionControl(audio_do);
    }

    if(audio_do != nullptr){
        QString zone = search_control_zone(word);
        audio_do->_audio_area_zone = zone;
    }

    return audio_do;
}
